//

//  WWPullDownButtonView.m

//  YiQiTingShu

//

//  Created by WeiWei on 15/11/3.

//  Copyright © 2015年 直到世界的尽头. All rights reserved.

//



#import "WWPullDownButtonView.h"



@interface WWPullDownButtonView ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>



@property(nonatomic, assign)CGRect rect;

@property(nonatomic, assign)BOOL isTap;

@property(nonatomic, strong)UICollectionView *collectionView;



@end



static NSString *identifier = @"cell";



@implementation WWPullDownButtonView



#pragma mark - 懒加载



-(UILabel *)titleLabel

{
    
    if (!_titleLabel) {
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _rect.size.width, _rect.size.height)];
        
        _titleLabel.textAlignment = 1;
        
        _titleLabel.font = [UIFont systemFontOfSize:16 + FitFont];
        [_titleLabel NightWithType:UIViewColorTypeNormal];
        
        [self addSubview:_titleLabel];
        
    }
    
    return _titleLabel;
    
}



-(UICollectionView *)collectionView

{
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        
        flowLayout.sectionInset = UIEdgeInsetsMake(FitConstant_YorHeight(15), FitConstant_XorWidth(15), FitConstant_XorWidth(15), FitConstant_YorHeight(15));
        
        flowLayout.minimumInteritemSpacing = FitConstant_XorWidth(15);
        
        flowLayout.minimumLineSpacing = FitConstant_YorHeight(15);
        
        
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(FitConstant_XorWidth(-290), FitConstant_YorHeight(667) - 64 - _backView_Height, FitConstant_XorWidth(375), 0) collectionViewLayout:flowLayout];
        
        _collectionView.delegate = self;
        
        _collectionView.dataSource = self;
        
//        _collectionView.userInteractionEnabled = YES;
        
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        [_collectionView NightWithType:UIViewColorTypeNormal];
        
        [_collectionView registerClass:[WHNCollectionViewCell class] forCellWithReuseIdentifier:identifier];
        
        [self.controllerView addSubview:_collectionView];
        
    }
    
    return _collectionView;
    
}



#pragma mark - 初始化

-(instancetype)initWithFrame:(CGRect)frame WithTitle:(NSString *)title WithInfoArray:(NSArray *)array WithSuperView:(UIView *)view WithHeight:(CGFloat)height WithSupTableView:(UIView *)tableView

{
    
    if (self = [super initWithFrame:frame]) {
        
//        self.userInteractionEnabled = YES;
        
        _rect = frame;
        
        self.titleLabel.text = title;
        
        self.dataSource = array;
        
        _backView_Height = height;
        
        _controllerView = view;
        
        _supTableView = tableView;
        
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        
        [self addGestureRecognizer:tap];
        
        [self.collectionView reloadData];
        
    }
    
    return self;
    
}



#pragma mark - 点击方法

-(void)tapAction

{
    
    //NSLog(@"tap");
    
    if (!_isTap) {
        
        [UIView animateWithDuration:.5 animations:^{
            
            self.collectionView.frame = CGRectMake(FitConstant_XorWidth(-290), FitConstant_YorHeight(667) - FitConstant_YorHeight(64) - _backView_Height, FitConstant_XorWidth(375), _backView_Height);

//            _supTableView.userInteractionEnabled = NO;
            
        } completion:^(BOOL finished) {
            
            [_collectionView reloadData];
            
        }];
        
        ;
        
    }
    
    else{
        
        [UIView animateWithDuration:.5 animations:^{
            
            self.collectionView.frame = CGRectMake(FitConstant_XorWidth(-290), FitConstant_YorHeight(667) - FitConstant_YorHeight(64) - _backView_Height, FitConstant_XorWidth(375), 0);
            
//            _supTableView.userInteractionEnabled = NO;
            
        } completion:^(BOOL finished) {
            
            [_collectionView reloadData];
            
        }];
        
    }
    
    _isTap = !_isTap;
    
}



-(void)changeDataSource:(NSArray *)dataSource

{
    
    _dataSource = dataSource;
    
    [_collectionView reloadData];
    
}



#pragma mark - collectionView代理方法

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{
    
    return _dataSource.count;
    
}



-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{
    
    WHNCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
    cell.kind = WHNCollectionViewKindOneLabel;
//    cell.userInteractionEnabled = YES;
    cell.titleLabel.text = _dataSource[indexPath.row];
//    cell.titleLabel.userInteractionEnabled = YES;
    
    return cell;
    
}



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{
    
    return CGSizeMake(FitConstant_XorWidth(100), FitConstant_YorHeight(40));
    
}



-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{
    NSLog(@"ww");
//    _supTableView.userInteractionEnabled = YES;
    
    [self.delegate didSelectionPullButton:indexPath.row];
    
    [self tapAction];
    
}



@end

